// 测试优化效果的示例代码
// 注意：这是示例代码，实际使用时需要在Spring Boot测试环境中运行

@Test
public void testOptimizedQuery() {
    // 构建测试用户信息
    UserProfileInfo profile = new UserProfileInfo();
    profile.setProvince("北京");
    profile.setYear(2024);
    profile.setTypeName("物理类");
    profile.setSubjects(Arrays.asList("物理", "化学", "生物"));
    profile.setInterestedMajorCategories(Arrays.asList("计算机", "软件工程"));
    profile.setTotalScore(600);

    // 记录开始时间
    long startTime = System.currentTimeMillis();
    
    // 使用优化版本查询
    List<MajorAdmissionInfo> optimizedResult = userProfileService
            .queryAdmissionScoresWithEnrollmentPlanOptimized(profile);
    
    long optimizedTime = System.currentTimeMillis() - startTime;
    
    // 记录开始时间
    startTime = System.currentTimeMillis();
    
    // 使用原有两步查询（用于对比）
    List<CollegeEnrollmentPlanDO> enrollmentPlans = userProfileService
            .filterEnrollmentPlansByConditions(profile);
    List<MajorAdmissionInfo> originalResult = userProfileService
            .queryAdmissionScoresByEnrollmentPlans(enrollmentPlans, profile);
    
    long originalTime = System.currentTimeMillis() - startTime;
    
    // 输出对比结果
    System.out.println("=== 查询性能对比 ===");
    System.out.println("优化版本耗时: " + optimizedTime + "ms");
    System.out.println("原有版本耗时: " + originalTime + "ms");
    System.out.println("性能提升: " + ((originalTime - optimizedTime) * 100.0 / originalTime) + "%");
    
    System.out.println("\n=== 查询结果对比 ===");
    System.out.println("优化版本结果数量: " + optimizedResult.size());
    System.out.println("原有版本结果数量: " + originalResult.size());
    
    // 验证结果一致性（简单验证）
    Set<String> optimizedKeys = optimizedResult.stream()
            .map(info -> info.getSchoolName() + ":" + info.getMajorName())
            .collect(Collectors.toSet());
    
    Set<String> originalKeys = originalResult.stream()
            .map(info -> info.getSchoolName() + ":" + info.getMajorName())
            .collect(Collectors.toSet());
    
    System.out.println("结果一致性: " + (optimizedKeys.equals(originalKeys) ? "一致" : "不一致"));
    
    if (!optimizedKeys.equals(originalKeys)) {
        System.out.println("优化版本独有: " + 
                optimizedKeys.stream()
                        .filter(key -> !originalKeys.contains(key))
                        .collect(Collectors.toList()));
        
        System.out.println("原有版本独有: " + 
                originalKeys.stream()
                        .filter(key -> !optimizedKeys.contains(key))
                        .collect(Collectors.toList()));
    }
}

// 测试不同场景下的性能表现
@Test
public void testOptimizedQueryWithDifferentScenarios() {
    // 场景1：大量意向专业
    UserProfileInfo profile1 = new UserProfileInfo();
    profile1.setProvince("广东");
    profile1.setYear(2024);
    profile1.setTypeName("物理类");
    profile1.setSubjects(Arrays.asList("物理", "化学"));
    profile1.setInterestedMajorCategories(Arrays.asList(
            "计算机", "软件工程", "电子信息", "通信工程", "自动化", 
            "机械工程", "电气工程", "材料科学", "化学工程", "生物工程"));
    
    testScenario("大量意向专业场景", profile1);
    
    // 场景2：无意向专业
    UserProfileInfo profile2 = new UserProfileInfo();
    profile2.setProvince("江苏");
    profile2.setYear(2024);
    profile2.setTypeName("历史类");
    profile2.setSubjects(Arrays.asList("历史", "政治", "地理"));
    profile2.setInterestedMajorCategories(new ArrayList<>());
    
    testScenario("无意向专业场景", profile2);
    
    // 场景3：复杂选科要求
    UserProfileInfo profile3 = new UserProfileInfo();
    profile3.setProvince("浙江");
    profile3.setYear(2024);
    profile3.setSubjects(Arrays.asList("物理", "化学", "生物", "技术"));
    profile3.setInterestedMajorCategories(Arrays.asList("医学", "生物科学"));
    
    testScenario("复杂选科要求场景", profile3);
}

private void testScenario(String scenarioName, UserProfileInfo profile) {
    System.out.println("\n=== " + scenarioName + " ===");
    
    long startTime = System.currentTimeMillis();
    List<MajorAdmissionInfo> result = userProfileService
            .queryAdmissionScoresWithEnrollmentPlanOptimized(profile);
    long endTime = System.currentTimeMillis();
    
    System.out.println("查询耗时: " + (endTime - startTime) + "ms");
    System.out.println("结果数量: " + result.size());
    
    if (!result.isEmpty()) {
        System.out.println("示例结果: " + 
                result.get(0).getSchoolName() + " - " + result.get(0).getMajorName());
    }
}
